home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3 / CHAPTER3 / WINFRPT.C < prev    next >
C/C++ Source or Header  |  1996-03-06  |  8KB  |  285 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "winfrpt.h"  
  5.  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19. HWND      hMainWnd;
  20.  
  21. LPCTSTR lpszAppName = "MyApp";
  22. LPCTSTR lpszTitle   = "TTM_WINDOWFROMPOINT"; 
  23.  
  24.  
  25. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  26.  
  27.  
  28. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  29.                       LPTSTR lpCmdLine, int nCmdShow)
  30. {
  31.    MSG      msg;
  32.    HWND     hWnd; 
  33.    WNDCLASS wc;
  34.  
  35.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  36.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  37.    wc.cbClsExtra    = 0;                      
  38.    wc.cbWndExtra    = 0;                      
  39.    wc.hInstance     = hInstance;              
  40.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  41.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  42.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  43.    wc.lpszMenuName  = lpszAppName;              
  44.    wc.lpszClassName = lpszAppName;              
  45.  
  46.    if ( IS_WIN95 )
  47.    {
  48.       if ( !RegisterWin95( &wc ) )
  49.          return( FALSE );
  50.    }
  51.    else if ( !RegisterClass( &wc ) )
  52.       return( FALSE );
  53.  
  54.    hInst = hInstance; 
  55.  
  56.    hWnd = CreateWindow( lpszAppName, 
  57.                         lpszTitle,    
  58.                         WS_OVERLAPPEDWINDOW, 
  59.                         CW_USEDEFAULT, 0, 
  60.                         CW_USEDEFAULT, 0,  
  61.                         NULL,              
  62.                         NULL,              
  63.                         hInstance,         
  64.                         NULL               
  65.                       );
  66.  
  67.    if ( !hWnd ) 
  68.       return( FALSE );
  69.  
  70.    hMainWnd = hWnd;
  71.  
  72.    ShowWindow( hWnd, nCmdShow ); 
  73.    UpdateWindow( hWnd );         
  74.  
  75.    while( GetMessage( &msg, NULL, 0, 0) )   
  76.    {
  77.       TranslateMessage( &msg ); 
  78.       DispatchMessage( &msg );  
  79.    }
  80.  
  81.    return( msg.wParam ); 
  82. }
  83.  
  84.  
  85. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  86. {
  87.    WNDCLASSEX wcex;
  88.  
  89.    wcex.style         = lpwc->style;
  90.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  91.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  92.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  93.    wcex.hInstance     = lpwc->hInstance;
  94.    wcex.hIcon         = lpwc->hIcon;
  95.    wcex.hCursor       = lpwc->hCursor;
  96.    wcex.hbrBackground = lpwc->hbrBackground;
  97.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  98.    wcex.lpszClassName = lpwc->lpszClassName;
  99.  
  100.    // Added elements for Windows 95.
  101.    //...............................
  102.    wcex.cbSize = sizeof(WNDCLASSEX);
  103.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  104.                             IMAGE_ICON, 16, 16,
  105.                             LR_DEFAULTCOLOR );
  106.             
  107.    return RegisterClassEx( &wcex );
  108. }
  109.  
  110. WNDPROC OldTipProc = NULL;
  111.  
  112. LRESULT CALLBACK TipProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  113. {
  114. static LPPOINT  lpPoint;
  115.  
  116.    switch( uMsg )
  117.    {
  118.       case TTM_WINDOWFROMPOINT :
  119.            lpPoint = (LPPOINT)lParam; 
  120.  
  121.            // Modify the coordinates for the window
  122.            // that the tool tip should be displayed for.
  123.            
  124.  
  125.            // Return the window handle that 
  126.            // contains the coordinates.
  127.            //..............................
  128.            return( (LRESULT)hMainWnd );
  129.  
  130.       default : 
  131.            return( CallWindowProc( OldTipProc, hWnd, uMsg, wParam, lParam ) );
  132.    }
  133.  
  134.    return( 0L );
  135. }
  136.  
  137.  
  138. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  139. {
  140. static TOOLINFO ti;
  141. static int      index;
  142. static HWND     hToolTip = NULL;
  143.  
  144.    switch( uMsg )
  145.    {
  146.       case WM_CREATE :
  147.               {
  148.                  InitCommonControls();
  149.  
  150.                  // Create tool tip control.
  151.                  //.........................
  152.                  hToolTip = CreateWindowEx( 0,
  153.                                             TOOLTIPS_CLASS, NULL, 
  154.                                             TTS_ALWAYSTIP,
  155.                                             CW_USEDEFAULT, 
  156.                                             CW_USEDEFAULT, 
  157.                                             CW_USEDEFAULT, 
  158.                                             CW_USEDEFAULT,
  159.                                             hWnd, NULL, 
  160.                                             hInst, NULL );
  161.  
  162.                  // Subclass the tool tip.
  163.                  //.......................
  164.                  OldTipProc = (WNDPROC)SetWindowLong( hToolTip, GWL_WNDPROC, (LONG)TipProc );
  165.  
  166.                  // Add the tools to the tool tip control.
  167.                  //.......................................
  168.                  ti.cbSize = sizeof( TOOLINFO );
  169.                  ti.hwnd   = hWnd;
  170.                  ti.hinst  = hInst;
  171.                  ti.uFlags = TTF_SUBCLASS;
  172.  
  173.                  for( index=0; index < 4; index++ )
  174.                  {
  175.                     ti.uId         = index+1;
  176.                     ti.lpszText    = (LPTSTR)index+1;
  177.                     ti.rect.top    = 0;
  178.                     ti.rect.bottom = 10;
  179.                     ti.rect.left   = 10*index;
  180.                     ti.rect.right  = 10*(index+1);
  181.  
  182.                     SendMessage( hToolTip, TTM_ADDTOOL, 0, (LPARAM)&ti );
  183.                  }
  184.               }
  185.               break;
  186.  
  187.       case WM_SIZE :
  188.               {
  189.                  // Adjust the tool rectangles for the new
  190.                  // section size.
  191.                  //.......................................
  192.                  RECT     rect;
  193.  
  194.                  index = 0;
  195.  
  196.                  ti.cbSize = sizeof( TOOLINFO );
  197.  
  198.                  GetClientRect( hWnd, &rect );
  199.  
  200.                  while ( SendMessage( hToolTip, TTM_ENUMTOOLS, 
  201.                          index, (LPARAM)&ti ) )
  202.                  {
  203.                     ti.rect.top    = 0;
  204.                     ti.rect.bottom = HIWORD( lParam );
  205.                     ti.rect.left   = (rect.right/4)*index;
  206.                     ti.rect.right  = (rect.right/4)*(index+1);
  207.  
  208.                     SendMessage( hToolTip, TTM_SETTOOLINFO, index, (LPARAM)&ti );
  209.  
  210.                     index++;
  211.                  }
  212.               }
  213.               break;
  214.  
  215.       case WM_PAINT  :
  216.               {
  217.                  // Paint the vertical lines to denote the sections.
  218.                  //.................................................
  219.                  PAINTSTRUCT ps;
  220.  
  221.                  index = 0;
  222.  
  223.                  BeginPaint( hWnd, &ps );
  224.  
  225.                  while ( SendMessage( hToolTip, TTM_ENUMTOOLS, 
  226.                          index, (LPARAM)&ti ) )
  227.                  {
  228.                     MoveToEx( ps.hdc, ti.rect.right, ti.rect.top, NULL );
  229.                     LineTo( ps.hdc, ti.rect.right, ti.rect.bottom );
  230.                     index++;
  231.                  }
  232.  
  233.                  EndPaint( hWnd, &ps );
  234.               }
  235.               break;
  236.  
  237.  
  238.       case WM_COMMAND :
  239.               switch( LOWORD( wParam ) )
  240.               {
  241.                  case IDM_ABOUT :
  242.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  243.                         break;
  244.  
  245.                  case IDM_EXIT :
  246.                         DestroyWindow( hWnd );
  247.                         break;
  248.               }
  249.               break;
  250.       
  251.       case WM_DESTROY :
  252.               PostQuitMessage(0);
  253.               break;
  254.  
  255.       default :
  256.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  257.    }
  258.  
  259.    return( 0L );
  260. }
  261.  
  262.  
  263. LRESULT CALLBACK About( HWND hDlg,           
  264.                         UINT message,        
  265.                         WPARAM wParam,       
  266.                         LPARAM lParam)
  267. {
  268.    switch (message) 
  269.    {
  270.        case WM_INITDIALOG: 
  271.                return (TRUE);
  272.  
  273.        case WM_COMMAND:                              
  274.                if (   LOWORD(wParam) == IDOK         
  275.                    || LOWORD(wParam) == IDCANCEL)    
  276.                {
  277.                        EndDialog(hDlg, TRUE);        
  278.                        return (TRUE);
  279.                }
  280.                break;
  281.    }
  282.  
  283.    return (FALSE); 
  284. }
  285.